//
//  GMRankVC.m
//  Teecloud
//
//  Created by 小熊 on 2018/10/16.
//  Copyright © 2018年 小志. All rights reserved.
//

#import "GMRankVC.h"
#import "GMRankModel.h"
#import "GMRankCell.h"
#import "GMRankDetailVC.h"
@interface GMRankVC ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *tabView;
@property (nonatomic, assign) NSInteger pageIndex;
@property (nonatomic, strong) NSMutableArray *dataArr;

@end

@implementation GMRankVC
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self getRankList];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kWhiteColor;
    [self.tabView registerNib:[UINib nibWithNibName:@"GMRankCell" bundle:nil] forCellReuseIdentifier:@"GMRankCell"];
}
#pragma mark - MJRefresh
-(void)getRankList{
    self.pageIndex =0;
    NSDictionary *dict= @{@"from":FROM_IOS,
                          @"busiOrgId":[XGMArchiverTools cacheForKey:BUSIORGID],
                          @"loginFinger":[XGMArchiverTools cacheForKey:USER_identifierNumber],
                          @"userId":[XGMArchiverTools cacheForKey:USER_UserId],
                          @"pageIndex":@(self.pageIndex)
                          };
    kWeakObject(self)
    [HJNetwork HTTPWithMethod:HJRequestMethodPOST url:[NSString stringWithFormat:@"ajax-api.ashx?$c=TeeyunApp&$m=GetRankList&$v=%@",VERSION] parameters:dict cachePolicy:HJCachePolicyIgnoreCache callback:^(id responseObject, NSError *error) {

        if (!error) {
            if ([[responseObject objectForKey:RESULT_NAME_Success] boolValue]) {
                NSArray *arr = [GMRankModel mj_objectArrayWithKeyValuesArray:[responseObject objectForKey:RESULT_NAME_Data]];
                [self.dataArr removeAllObjects];
                [self.dataArr addObjectsFromArray:arr];
                [weakself.tabView.mj_footer endRefreshing];
                [self.tabView reloadData];
            }else{
                [MBProgressHUD showErrorOrSuc:[responseObject objectForKey:RESULT_NAME_Message] toView:weakself.view];
            }
        }else{
            [MBProgressHUD showErrorOrSuc:@"网络异常" toView:weakself.view];
        }
    }];
}
- (void)getMoreRankList{
    self.pageIndex ++;
    NSDictionary *dict= @{@"from":FROM_IOS,
                          @"busiOrgId":[XGMArchiverTools cacheForKey:BUSIORGID],
                          @"loginFinger":[XGMArchiverTools cacheForKey:USER_identifierNumber],
                          @"userId":[XGMArchiverTools cacheForKey:USER_UserId],
                          @"pageIndex":@(self.pageIndex)
                          };
    kWeakObject(self)
    [HJNetwork POSTWithURL:[NSString stringWithFormat:@"ajax-api.ashx?$c=TeeyunApp&$m=GetRankList&$v=%@",VERSION] parameters:dict cachePolicy:HJCachePolicyIgnoreCache callback:^(id responseObject, NSError *error) {
        [MBProgressHUD hideHUDForView:weakself.view animated:YES];
        if (!error) {
            if ([[responseObject objectForKey:RESULT_NAME_Success] boolValue]) {
                NSArray *arr = [GMRankModel mj_objectArrayWithKeyValuesArray:[responseObject objectForKey:RESULT_NAME_Data]];
                [self.dataArr addObjectsFromArray:arr];
                if (arr.count>0) {
                    [weakself.tabView.mj_footer endRefreshing];
                    [weakself.tabView reloadData];
                }else{
                    weakself.tabView.mj_footer.state = MJRefreshStateNoMoreData;
                }
            }else{
                [weakself.tabView.mj_footer endRefreshing];

                [MBProgressHUD showErrorOrSuc:[responseObject objectForKey:RESULT_NAME_Message] toView:weakself.view];
            }
        }else{
            [weakself.tabView.mj_footer endRefreshing];

            [MBProgressHUD showErrorOrSuc:@"网络异常" toView:weakself.view];
        }
    }];
}
-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc] init];
    }
    return _dataArr;
}
-(UITableView *)tabView{
    if (!_tabView) {
        _tabView = [UITableView zj_tableViewWithSuperview:self.view delegate:self style:UITableViewStylePlain constraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.view);
        }];
        _tabView.tableFooterView = [UIView new];
        _tabView.separatorStyle =UITableViewCellSeparatorStyleNone;
        _tabView.showsVerticalScrollIndicator = false;
        _tabView.showsHorizontalScrollIndicator = false;
        _tabView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(getMoreRankList)];
        _tabView.mj_footer.hidden =YES;
    }
    return _tabView;
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    self.tabView.mj_footer.hidden = self.dataArr.count > 0? NO:YES;
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GMRankCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GMRankCell" forIndexPath:indexPath];
    GMRankModel *model = self.dataArr[indexPath.row];
    cell.model = model;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    GMRankModel *model = self.dataArr[indexPath.row];
    GMRankDetailVC *rankDetailVC = [[GMRankDetailVC alloc]init];
    rankDetailVC.titleStr = model.Title;
    rankDetailVC.rankId = model.Id;
    [self.navigationController pushViewController:rankDetailVC animated:true];
}

@end
